package com.feishi.core.util.common.lecode.thread;

import java.util.function.IntConsumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2020-08-18
 * Time: 下午2:38
 */
public class FizzBuzz {
    private int n;

    public FizzBuzz(int n) {
        this.n = n;
    }

    int i = 1;

    public static void main(String[] args) {
        FizzBuzz fizzBuzz = new FizzBuzz(15);
        new Thread(() -> {
            try {
                fizzBuzz.fizz(() -> {
                    System.err.print(" fizz");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                fizzBuzz.buzz(() -> {
                    System.err.print(" buzz");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                fizzBuzz.fizzbuzz(() -> {
                    System.err.print(" fizzbuzz");
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                fizzBuzz.number((c) -> {
                    System.err.print(" "+c);
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        synchronized (this) {
            while (i <= n) {

                if (i % 3 == 0 && i % 5 != 0) {
                    printFizz.run();
                    i++;
                    this.notifyAll();
                    continue;
                }
//                System.out.println("_________________"+41);
                this.wait();
//                System.out.println("_________________"+42);
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        synchronized (this) {
            while (i <= n) {

                if (i % 3 != 0 && i % 5 == 0) {
                    printBuzz.run();
                    i++;
                    this.notifyAll();
                    continue;
                }
//                System.out.println("_________________"+31);
                this.wait();
//                System.out.println("_________________"+32);
            }
        }

    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        synchronized (this) {
            while (i <= n) {

                if (i % 3 == 0 && i % 5 == 0) {
                    printFizzBuzz.run();
                    i++;
                    this.notifyAll();
                    continue;
                }
//                System.out.println("_________________"+21);
                this.wait();
//                System.out.println("_________________"+22);
            }
        }

    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        synchronized (this) {
            while (i <= n) {
                if (i % 3 != 0 && i % 5 != 0) {
                    printNumber.accept(i);
                    i++;
                    this.notifyAll();
                    continue;
                }
//                System.out.println("_________________"+11);
                this.wait();
//                System.out.println("_________________"+12);
            }

        }

    }
}